﻿namespace Yom.WebService.Admin
{
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class AdminConfig : AdminBase
    {
        public void OutWrite()
        {
            string Action = Factory.Request("Action");

            switch (Action)
            {
                case "Edit":
                    string SiteTitle = Factory.Request("SiteTitle");
                    string SiteUrl = Factory.Request("SiteUrl");
                    string KeyWords = Factory.Request("KeyWords");
                    string Description = Factory.Request("Description");
                    string BottomDesc = Factory.Request("BottomDesc");
                    string CopyRight = Factory.Request("CopyRight");
                    string Theme = Factory.Request("Theme");
                    string Locale = Factory.Request("Locale");
                    string Ext = Factory.Request("Ext");
                    string SaveFace = Factory.Request("SaveFace");
                    string LoginName = Factory.Request("LoginName");
                    string UserName = Factory.Request("UserName");
                    string Email = Factory.Request("Email");
                    string LoginPass = Factory.Request("LoginPass");
                    if (!string.IsNullOrEmpty(SiteTitle) && !string.IsNullOrEmpty(SiteUrl) && !string.IsNullOrEmpty(Theme) && !string.IsNullOrEmpty(Ext) && !string.IsNullOrEmpty(SaveFace) && !string.IsNullOrEmpty(Locale))
                    {
                        StringBuilder strSQL = new StringBuilder();
                        strSQL.Append("UPDATE [Yom_Setting] SET");
                        strSQL.Append("[BottomDesc]='" + BottomDesc.Replace("'", @"\'") + "',[CopyRight]='" + CopyRight.Replace("'", @"\'") + "',[Description]='" + Description.Replace("'", @"\'") + "',");
                        strSQL.Append("[Ext]='" + Ext + "',[KeyWords]='" + KeyWords.Replace("'", @"\'") + "',[Locale]='" + Locale + "',[SaveFace]='" + SaveFace + "',[SiteTitle]='" + SiteTitle.Replace("'", @"\'") + "',[SiteUrl]='" + SiteUrl + "',");
                        strSQL.Append("[Theme]='" + Theme + "' WHERE [SetID]=1");
                        base.Response.Write((new Yom.Data.Setting().Query(strSQL.ToString())) ? "1" : "0");
                        if (!string.IsNullOrEmpty(LoginPass))
                        {
                            Yom.Model.Admin model = new Yom.Model.Admin();
                            model.AdminID = SiteLoad.Admin.AdminID;
                            model.Email = Email;
                            model.LoginName = LoginName;
                            model.LoginPass = LoginPass.ToMd5();
                            model.UserName = UserName;
                            if (new Yom.Data.Admin().Update(model))
                            {
                                SiteLoad.Admin = new Yom.Model.Admin();
                                HttpContext.Current.Response.Cookies.Add(new HttpCookie(SiteLoad.Cookie + "_Admin", ""));
                            }
                        }
                    }
                    break;
                case "Clear":
                    Caches caches = new Caches();
                    caches.ClearCache();
                    Yom.Template.MakeHtml.MakeIndex();
                    base.Response.Write("1");
                    break;
                default:
                    JsonModel Js = new JsonModel();
                    StringBuilder builder = new StringBuilder();
                    Js.Add("Title", "站点基本设置 - 设置基本信息");
                    builder.Append("<div class=\"SubMenu\"><a href=\"javascript:$.Run(2)\">设置基本信息</a> | <a href=\"javascript:$.Run(2,1,0)\">清理服务器缓存</a></div><div id=\"ErrShow\" style=\"text-align:center;color:red\"></div>");
                    builder.Append("<fieldset class=\"config\"><legend> 站点基本信息 </legend>");
                    builder.Append("<dl><dd>站点名称</dd><dt><input id=\"title\" type=\"text\" class=\"text\" value=\"" + SiteLoad.Scheme.SiteTitle + "\"/></dt></dl>");
                    builder.Append("<dl><dd>站点地址<div class=\"shuom\">最后不要加 /</div></dd><dt><input id=\"url\" type=\"text\" class=\"text\" value=\"" + SiteLoad.Scheme.SiteUrl + "\"/></dt></dl>");
                    builder.Append("<dl><dd>Meta 关键字</dd><dt><textarea id=\"keywords\" cols=\"50\" rows=\"5\">" + SiteLoad.Scheme.KeyWords + "</textarea></dt></dl>");
                    builder.Append("<dl><dd>Meta 描述</dd><dt><textarea id=\"description\" cols=\"50\" rows=\"5\">" + SiteLoad.Scheme.Description + "</textarea></dt></dl>");
                    builder.Append("<dl><dd>底部描述<div class=\"shuom\">在底部对站点漫画进行说明</div></dd><dt><textarea id=\"bottomdesc\" cols=\"50\" rows=\"5\">" + SiteLoad.Scheme.BottomDesc + "</textarea></dt></dl>");
                    builder.Append("<dl><dd>版权说明</dd><dt><textarea id=\"copyright\" cols=\"50\" rows=\"5\">" + SiteLoad.Scheme.CopyRight + "</textarea></dt></dl></fieldset>");
                    builder.Append("<fieldset class=\"config\"><legend> 站点基本配置 </legend>");
                    builder.Append("<dl><dd>模板选择</dd><dt>&nbsp;&nbsp;<select id=\"theme\">");
                    string[] dirList = Factory.ThemeList();
                    foreach (string dir in dirList)
                    {
                        if (dir != ".svn")
                            builder.Append("<option value=\"" + dir + "\">" + dir + "</option>");
                    }
                    builder.Append("</select></dt></dl>");
                    builder.Append("<dl><dd>语言选择</dd><dt>&nbsp;&nbsp;<select id=\"locale\">");
                    string[] localeList = Factory.LocaleList();
                    foreach (string locale in localeList)
                    {
                        builder.Append("<option value=\"" + locale + "\">" + locale + "</option>");
                    }
                    builder.Append("</select></dt></dl>");
                    builder.Append("<dl><dd>静态后缀</dd><dt><input id=\"ext\" type=\"text\" class=\"text\" value=\"" + SiteLoad.Scheme.Ext + "\"/></dt></dl>");
                    builder.Append("<dl><dd>封面保存目录<div class=\"shuom\">使用相对路径</div></dd><dt><input id=\"saveface\" type=\"text\" class=\"text\" value=\"" + SiteLoad.Scheme.SaveFace + "\"/></dt></dl></fieldset>");
                    builder.Append("<fieldset class=\"config\"><legend> 管理员信息 </legend>");
                    builder.Append("<dl><dd>登录名称</dd><dt><input id=\"loginname\" type=\"text\" class=\"text\" value=\"" + SiteLoad.Admin.LoginName + "\" /></dt></dl>");
                    builder.Append("<dl><dd>管理员昵称</dd><dt><input id=\"username\" type=\"text\" class=\"text\" value=\"" + SiteLoad.Admin.UserName + "\" /></dt></dl>");
                    builder.Append("<dl><dd>管理员邮件</dd><dt><input id=\"email\" type=\"text\" class=\"text\" value=\"" + SiteLoad.Admin.Email + "\" /></dt></dl>");
                    builder.Append("<dl><dd>登录密码</dd><dt><input id=\"loginpass\" type=\"password\" class=\"text\" /></dt></dl>");
                    builder.Append("<div class=\"SubButton\"><input type=\"button\" id=\"subConfig\" value=\"保存配置\" class=\"button\"/></div>");
                    Js.Add("Panel", builder.ToString());
                    base.Response.Write(new Json(Js));
                    break;
            }
        }
    }
}